package com.zc.framework.utils;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created by zc on 2016/3/22.
 */
public class JSONUtils {

    public static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static String writeValueAsString(Object object) throws IOException {
        return mapper.writeValueAsString(object);
    }

    public static Object readJson2Obj(String json, Class valueType) throws IOException {
        return mapper.readValue(json,valueType);
    }

    public static Object readJson2ListOfObj(String json,  TypeReference<?> valueTypeRef) throws IOException {
        return mapper.readValue(json,valueTypeRef);
    }
}
